package com.example.demo.Controller;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.example.demo.Dao.PersonRepository;
import com.example.demo.Dao.RecordRepository;
import com.example.demo.Entity.Person;
import com.example.demo.Entity.Record;
import com.example.demo.Utils.CSVUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/download")
@CrossOrigin
public class DownloadController {
    @Resource
    PersonRepository personRepository;
    @Resource
    RecordRepository recordRepository;
    @GetMapping("getpersoncsv")
    public void getPersonCsv(HttpServletResponse response){
        try {
            CSVUtils.downloadPerson(response, Person.class,personRepository.findAll(),"社区人员情况表.xls");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @GetMapping("getrecordcsv")
    public void getRecordCsv(HttpServletResponse response){
        try {
            CSVUtils.downloadPerson(response, Record.class,recordRepository.findAll(),"检测记录表.xls");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
